﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Extensions;
using ConfExpert.Utility;

namespace Tailord
{
    public partial class FaturaOlustur : XPopupForm
    {
        int faturaNo,odemeTur;
        decimal ozelFiyat;

        public decimal OzelFiyat
        {
            get { return ozelFiyat; }
            set { ozelFiyat = value; }
        }
        bool isProva = false;
        
        public bool IsProva
        {
            get { return isProva; }
        }

        public int FaturaNo
        {
            get { return faturaNo; }
        }

        public int OdemeTur
        {
            get { return odemeTur; }
        }

        public int FaturaTipi { get; set; }

        public FaturaOlustur(string odeme,bool IsProva,string siparisFiyati)
        {
            InitializeComponent();
            cbProva.Visible = IsProva;
            lblSiparisFiyati.Text = siparisFiyati;
            if (AppSession.UserInfo.UserId == 113 || AppSession.UserInfo.UserId == 1 || AppSession.UserInfo.UserId == 77 || AppSession.UserInfo.UserId == 79)
                lblOzelFiyat.Visible = txtOzelFiyat.Visible = true;

            if (odeme!="")
            {
                 cbOdeme.Enabled = false;
                switch(odeme)
                {
                    case "Kredi Kartı":
                        cbOdeme.SelectedIndex = 2;
                        return;
                    case "Nakit":
                        cbOdeme.SelectedIndex = 0;
                        return;
                    case "EFT":
                        cbOdeme.SelectedIndex = 1;
                        return;
                    default:
                        cbOdeme.Enabled = true;
                        cbOdeme.SelectedIndex = 0;
                        return;
                }                
            }
        }

        private void FaturaOlustur_Load(object sender, EventArgs e)
        {
            HeaderPanel.Visible = MainToolbar.Visible = lblHata.Visible = false;
            txtFatura.Focus();
            odemeTur = cbOdeme.SelectedIndex;
            rbFaturaTipi.SelectedIndex = 0;

            btnFatura.Click += btnFatura_Click;
            cbProva.CheckedChanged += cbProva_CheckedChanged;
        }

        void cbProva_CheckedChanged(object sender, EventArgs e)
        {
            isProva = cbProva.Checked;
        }


        void btnFatura_Click(object sender, EventArgs e)
        {
            lblHata.Visible = false;
            faturaNo = txtFatura.Text.ToIntZero();
            if (faturaNo==0)
            {
                lblHata.Text = "Lütfen fatura numarası girin";
                lblHata.Visible = true;
                return;
            }
            else if (faturaNo>0)
            {
                int isOk = AppSession.DataManager.ExecuteScalar("KontrolFaturaNo", faturaNo).ToIntZero();
                if (isOk == 1)
                {
                    lblHata.Text = "Belirttiğiniz fatura daha önceden kesilmiş";
                    lblHata.Visible = true;
                    return;
                }
                else
                {
                    Decimal.TryParse(txtOzelFiyat.Text, out ozelFiyat);
                    FaturaTipi = rbFaturaTipi.EditValue.ToIntZero();
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void cbOdeme_SelectedIndexChanged(object sender, EventArgs e)
        {
            odemeTur = cbOdeme.SelectedIndex;
        }
    }
}